package com.injurydetection.www.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.injurydetection.www.MyApplication;
import com.injurydetection.www.R;
import com.injurydetection.www.bean.DataBean;
import com.injurydetection.www.bean.ErjianRv;
import com.injurydetection.www.greendao.DataBeanDao;
import com.injurydetection.www.util.DateUtils;
import com.injurydetection.www.widget.DeleteDialog;

import java.util.Date;
import java.util.List;

/**
 * Created by sunguiyong on 2020/8/12
 */
public class ErjianAdapter extends RecyclerView.Adapter<ErjianAdapter.MyViewHolder> {
    private List<DataBean> mErjianRvList;
    private Context mContext;

    public ErjianAdapter(List<DataBean> erjianRvList, Context context) {
        mErjianRvList = erjianRvList;
        mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_erjian, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        long time1 = mErjianRvList.get(position).getCreateTime().getTime();
        long time2 = new Date().getTime();
        holder.time.setText(DateUtils.getDistanceTimeMy(time2, time1));
        String name = "";
        name = mErjianRvList.get(position).getName();
        if (name.length() > 6) {
            name = name.substring(0, 6) + "...";
        }
        holder.name.setText(name);
        holder.num.setText(mErjianRvList.get(position).getNumber());
        holder.xuexing.setText(mErjianRvList.get(position).getBloodType());
        holder.score.setText(mErjianRvList.get(position).getScore() + "分");
        int scoreLevel = mErjianRvList.get(position).getScoreLevel();
        String chuliStr = "";
        String shanghaiStr = "";

        switch (scoreLevel) {
            case 1:
                shanghaiStr = "轻伤";
                chuliStr = "常规处置";
                holder.score.setTextColor(Color.GREEN);
                holder.level.setTextColor(Color.GREEN);
                holder.chuzhi.setTextColor(Color.GREEN);
                break;
            case 2:
                shanghaiStr = "中度伤";
                chuliStr = "优先处置";
                holder.score.setTextColor(Color.YELLOW);
                holder.level.setTextColor(Color.YELLOW);
                holder.chuzhi.setTextColor(Color.YELLOW);
                break;
            case 3:
                shanghaiStr = "重症伤";
                chuliStr = "紧急处置";
                holder.score.setTextColor(Color.RED);
                holder.level.setTextColor(Color.RED);
                holder.chuzhi.setTextColor(Color.RED);
                break;
            case 4:
                shanghaiStr = "危重症伤";
                chuliStr = "期待处置";
                holder.score.setTextColor(Color.BLACK);
                holder.level.setTextColor(Color.BLACK);
                holder.chuzhi.setTextColor(Color.BLACK);
                holder.score.setBackgroundColor(Color.RED);
                holder.level.setBackgroundColor(Color.RED);
                holder.chuzhi.setBackgroundColor(Color.RED);
                break;
            default:
                shanghaiStr = "未知";
                break;
        }
        holder.level.setText(shanghaiStr);
        holder.chuzhi.setText(chuliStr);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mErjianRvList.size() == 0) {
                    return;
                }
                DeleteDialog deleteDialog = new DeleteDialog(mContext, R.style.TipDialog);
                deleteDialog.setCommitListener(new DeleteDialog.CommitListener() {
                    @Override
                    public void commit() {
                        //获取当前的databean
                        DataBean dataBean = mErjianRvList.get(position);

                        //设置databean的新值
                        dataBean.setId(mErjianRvList.get(position).getId());
                        dataBean.setIsChecked(false);

                        //更新操作
                        MyApplication.getDaoSession().getDataBeanDao().update(dataBean);
                        mErjianRvList.clear();
                        //再次查询最新二检数据
                        List<DataBean> listTemp = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                                .where(DataBeanDao.Properties.IsChecked.eq(true))
                                .orderAsc(DataBeanDao.Properties.CreateTime)
                                .build().list();

                        if (listTemp.size() > 0) {
                            for (int i1 = 0; i1 < listTemp.size(); i1++) {
                                if ((new Date().getTime() - listTemp.get(i1).getCreateTime().getTime()) / 1000 / 60 / 60 <= 5) {
                                    mErjianRvList.add(listTemp.get(i1));
                                }
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
                deleteDialog.show();
                deleteDialog.getWindow().setLayout(1000, 500);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mErjianRvList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView time, name, num, score, level, chuzhi, xuexing;
        ImageView delete;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.textView5);
            name = itemView.findViewById(R.id.textView27);
            num = itemView.findViewById(R.id.textView28);
            score = itemView.findViewById(R.id.textView29);
            level = itemView.findViewById(R.id.textView30);
            delete = itemView.findViewById(R.id.delete_img);
            chuzhi = itemView.findViewById(R.id.chuzhi);
            xuexing = itemView.findViewById(R.id.xuexing_tv);
        }
    }
}
